﻿using System.Linq;
using System.Data.Linq;
using UniMana.Data;
namespace UniMana.Data
{
    public static class Training_Subject_OpenData
    {
        public static void
            SetSubjectOpen_Close(this Table<Training_Subject_Open> subject_Open,
                            int subjectKey,
                            bool Close = true)
        {
            var subject_O = from sjo in subject_Open
                            where sjo.Training_SubjectKey == subjectKey &&
                                  sjo.Closed != Close
                            select sjo;
            foreach (var item in subject_O)
            {
                Training_Subject_OpenInfo info =
                    new Training_Subject_OpenInfo(item.Training_SubjectOpenKey);
                if (info != null)
                {
                    info.Properties.Closed = Close;
                    info.Save();
                }
            }
        }

        #region [An]
        /// <summary>
        /// Lấy môn học mở
        /// </summary>
        /// <param name="n_tso"></param>
        /// <param name="n_ts"></param>
        /// <returns></returns>

        public static IQueryable GetSubjectOpen(this Table<Training_Subject_Open> nTraining_Subject_Open,
            Table<Training_Subject> nTraining_Subject,
            Table<Training_TrainingProgram_Detail> nTraining_TrainingProgram_Detail,
            string m_strSubjectID)
        {
            var values = from t in nTraining_Subject
                         join t0 in nTraining_Subject_Open on t.Training_SubjectKey equals t0.Training_SubjectKey
                         join t1 in nTraining_TrainingProgram_Detail on t.Training_SubjectKey equals t1.Training_SubjectKey
                         where t0.Closed == false
                         select new
                         {
                             t.Catalogue_DepartmentKey,
                             t0.Training_SubjectKey,
                             t.ID,
                             t.Name,
                             t.Credits1,
                             t.Credits2,
                             t.Credits3,
                             t.Credits4,
                             t1.Training_TrainingProgramKey
                         };
            if (m_strSubjectID != "")
            {
                values = values.Where(tb => tb.ID.Contains(m_strSubjectID));
            }
            return values.Distinct();
        }

        /// <summary>
        /// Lấy mã môn học mở với mã môn học
        /// </summary>
        /// <param name="n_tso"></param>
        /// <param name="m_iTraining_SubjectKey"></param>
        /// <returns></returns>

        public static int GetSubjectOpenKey(this Table<Training_Subject_Open> nTraining_Subject_Open,
            int m_iTraining_SubjectKey)
        {
            var values = (from t in nTraining_Subject_Open
                          orderby
                            t.Training_SubjectOpenKey descending
                          where t.Training_SubjectKey == m_iTraining_SubjectKey
                          select new
                          {
                              t.Training_SubjectOpenKey
                          }).FirstOrDefault();
            return values.Training_SubjectOpenKey;
        }

        /// <summary>
        /// Kiểm tra môn học còn mở hay không
        /// </summary>
        /// <param name="nTraining_Subject_Open"></param>
        /// <param name="m_iTraining_SubjectKey"></param>
        /// <returns></returns>

        public static int CheckSubjectOpen(this Table<Training_Subject_Open> nTraining_Subject_Open,
            int m_iTraining_SubjectKey)
        {
            var values = (from t in nTraining_Subject_Open
                          orderby
                          t.Training_SubjectOpenKey descending
                          where t.Training_SubjectKey == m_iTraining_SubjectKey
                          select new
                          {
                              t.Closed
                          }).FirstOrDefault();
            return values.Closed == true ? 0 : 1;
        }

        // <summary>
        /// Lấy danh sách môn học đang mở
        /// </summary>
        /// <param name="nSubjectOpen"></param>
        /// <param name="nSubject"></param>
        /// <returns></returns>

        public static IQueryable GetListSubjectOpen(this Table<Training_Subject_Open> nTraining_Subject_Open,
            Table<Training_Subject> nTraining_Subject)
        {
            var values = from t in nTraining_Subject_Open
                         join t0 in nTraining_Subject on t.Training_SubjectKey equals t0.Training_SubjectKey
                         where
                           t.Closed == false
                         select new
                         {
                             Training_SubjectKey = (System.Int32?)t.Training_SubjectKey,
                             t0.ID,
                             t0.Name,
                             t0.Catalogue_DepartmentKey,
                             t0.TotalCredits,
                         };
            return values.Distinct();
        }
        #endregion
    }
}